package sample.plugin;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.List;
import java.util.jar.JarFile;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.shared.utils.io.FileUtils;

public class TestClass {
private static String JAR_PATH="jar:file:%s!/";
	public static void main(String[] args) throws Exception {
		File testDir = new File("/media/SSD/IISI/workspaces/myeclipse2014/hello-maven-plugin Maven Mojo/src/main/java");
		File testTargetDir = new File("/media/SSD/IISI/workspaces/myeclipse2014/SRIS OP TEST CASE/target/test-classes");
		final  String[] list = FileUtils.getFilesFromExtension(testDir.getAbsolutePath(), "java");
		final  String[] alist = FileUtils.getFilesFromExtension(testTargetDir.getAbsolutePath(), "class");
		final  String jarPath ="/maven3_repository/local/repo/sample/plugin/hello-maven-plugin/0.0.1-SNAPSHOT/hello-maven-plugin-0.0.1-SNAPSHOT.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.3/maven-plugin-tools-api-3.3.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar:/maven3_repository/local/repo/junit/junit/3.8.1/junit-3.8.1.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-annotations/3.3/maven-plugin-tools-annotations-3.3.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar:/maven3_repository/local/repo/org/ow2/asm/asm/5.0.2/asm-5.0.2.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-archiver/2.1.1/plexus-archiver-2.1.1.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-io/2.0.3/plexus-io-2.0.3.jar:/maven3_repository/local/repo/com/thoughtworks/qdox/qdox/1.12.1/qdox-1.12.1.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.3/maven-plugin-tools-generators-3.3.jar:/maven3_repository/local/repo/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar:/maven3_repository/local/repo/org/apache/maven/doxia/doxia-sink-api/1.0/doxia-sink-api-1.0.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.jar:/maven3_repository/local/repo/commons-collections/commons-collections/3.1/commons-collections-3.1.jar:/maven3_repository/local/repo/org/apache/velocity/velocity/1.7/velocity-1.7.jar:/maven3_repository/local/repo/commons-lang/commons-lang/2.4/commons-lang-2.4.jar:/maven3_repository/local/repo/org/ow2/asm/asm-commons/5.0.2/asm-commons-5.0.2.jar:/maven3_repository/local/repo/org/ow2/asm/asm-tree/5.0.2/asm-tree-5.0.2.jar:/maven3_repository/local/repo/net/sf/jtidy/jtidy/r938/jtidy-r938.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-java/3.3/maven-plugin-tools-java-3.3.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-model/3.3/maven-plugin-tools-model-3.3.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-script-beanshell/3.3/maven-script-beanshell-3.3.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-bsh-factory/1.0-alpha-7/plexus-bsh-factory-1.0-alpha-7.jar:/maven3_repository/local/repo/bsh/bsh/1.3.0/bsh-1.3.0.jar:/maven3_repository/local/repo/org/apache/maven/plugin-tools/maven-plugin-tools-javadoc/3.3/maven-plugin-tools-javadoc-3.3.jar:/opt/JAVA/jdk1.6.0_45/jre/../lib/tools.jar:/maven3_repository/local/repo/org/apache/maven/shared/maven-shared-io/1.1/maven-shared-io-1.1.jar:/maven3_repository/local/repo/org/apache/maven/shared/maven-shared-utils/0.7/maven-shared-utils-0.7.jar:/maven3_repository/local/repo/com/google/code/findbugs/jsr305/2.0.1/jsr305-2.0.1.jar:/maven3_repository/local/repo/org/apache/ant/ant/1.7.1/ant-1.7.1.jar:/maven3_repository/local/repo/org/apache/ant/ant-launcher/1.7.1/ant-launcher-1.7.1.jar:/maven3_repository/local/repo/commons-cli/commons-cli/1.0/commons-cli-1.0.jar:/maven3_repository/local/repo/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-4/plexus-interactivity-api-1.0-alpha-4.jar";
		final  String[] tokens = StringUtils.splitPreserveAllTokens(jarPath, ":");
		final URL[] jarUrl = new URL[ tokens.length];
		
		final	ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
		
		for (int i = 0; i < tokens.length; ++i) {
			final	JarFile jarFile = new JarFile(tokens[i]);
			jarUrl[i] = new URL(String.format(JAR_PATH, tokens[i]));
			URLClassLoader cl =  new URLClassLoader(new URL[]{jarUrl[i]}, parentClassLoader);;
			
		}
		final URL[] newURLs = (URL[])ArrayUtils.add(jarUrl,  testTargetDir.toURL());
		
	
		final   String classPath = extractClasspath(parentClassLoader);
		System.out.println("classPath: "+classPath);
		
//		final URLClassLoader classLoader= new URLClassLoader(new URL[] { testTargetDir.toURL() }, parentClassLoader);	
		final URLClassLoader classLoader= new URLClassLoader(newURLs, parentClassLoader);	
		
		
		classLoader.loadClass("tw.gov.moi.op.OpLoginTest001");
		  
		System.out.println(list.length);
	}

	private  static String extractClasspath(ClassLoader cl) {
		StringBuffer buf = new StringBuffer();
		String anotherCp = null;
		while (cl != null) {
			 
			if (cl instanceof URLClassLoader) {
				URL urls[] = ((URLClassLoader) cl).getURLs();
				for (int i = 0; i < urls.length; i++) {
					if (buf.length() > 0) {
						buf.append(File.pathSeparatorChar);
					}

					try {
						String decodedURL = URLDecoder.decode(urls[i].getFile()
								.toString(), "UTF-8");
						buf.append(decodedURL);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						buf.append(urls[i].getFile().toString());
					}
				}
			}
			cl = cl.getParent();
		}
		if (anotherCp != null) {
			return anotherCp;
		} else {
			return buf.toString();
		}
	}
}
